// 2025/10/21
// 完全二叉树的节点个数

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == nullptr)
            return 0;
        TreeNode *left = root, *right = root;
        int left_deep = 0, right_deep = 0;
        while(left)
        {
            left_deep++;
            left = left->left;
        }
        while(right)
        {
            right_deep++;
            right = right->right;
        }
        if(left_deep == right_deep)
            return (1 << left_deep) - 1;
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};